<template>
  <div>
    <p class="title">有序表格</p>
    <!-- <el-checkbox-group> -->
      <el-row :gutter="20">
        <el-col :span="2">
          <el-checkbox v-model="checked1" @change="orderlyTable('apple', 0, $event)">apple</el-checkbox>
        </el-col>
        <el-col :span="2">
          <el-checkbox v-model="checked2" @change="orderlyTable('bnana', 1, $event)">bnana</el-checkbox>
        </el-col>
        <el-col :span="2">
          <el-checkbox v-model="checked3" @change="orderlyTable('orange', 2, $event)">orange</el-checkbox>
        </el-col>
      </el-row>
    <!-- </el-checkbox-group> -->
    <el-table :data="tableData_1" border>
      <el-table-column label="FrulitName" prop="frulitName" width="200px"></el-table-column>
      <template v-for="(item, index) in orderlyTableList">
        <el-table-column v-if="item" :key="index" :label="item" :prop="item"></el-table-column>
      </template>
    </el-table>
    <p class="title">无序表格</p>
    <el-row :gutter="20">
      <el-col :span="2">
        <el-checkbox v-model="checked4" @change="disorderTable('apple', $event)">apple</el-checkbox>
      </el-col>
      <el-col :span="2">
        <el-checkbox v-model="checked5" @change="disorderTable('bnana', $event)">bnana</el-checkbox>
      </el-col>
      <el-col :span="2">
        <el-checkbox v-model="checked6" @change="disorderTable('orange', $event)">orange</el-checkbox>
      </el-col>
    </el-row>
    <el-table :data="tableData_2" border>
      <el-table-column label="FrulitName" prop="frulitName" width="200px"></el-table-column>
      <el-table-column v-for="(item, index) in disorderlyTableList" :key="index" :label="item" :prop="item"></el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData_1: [
        {
          frulitName: 'Variety-1'
        },
        {
          frulitName: 'Variety-2'
        }
      ],
      tableData_2: [
        {
          frulitName: 'Variety-1'
        },
        {
          frulitName: 'Variety-2'
        }
      ],
      checked1: false,
      checked2: false,
      checked3: false,
      checked4: false,
      checked5: false,
      checked6: false,
      orderlyTableList: [],
      disorderlyTableList: []
    }
  },
  mounted() {
  },
  methods: {
    orderlyTable(frult, order, $event) {
      if(!$event) {
        const index = this.orderlyTableList.findIndex(item => item === frult)
        this.orderlyTableList[index] = ''
        return
      }
      if(!this.orderlyTableList.includes(frult) && $event) {
        for(let i = 0; i <= order; i++) {
          if(i === order) {
            this.orderlyTableList[i] = frult
          }
        }
        this.tableData_1.forEach((item, index) => {
          item[frult] = frult + '-' + index
        })
      }
    },
    disorderTable(frult, $event) {
      if(!$event) {
        this.disorderlyTableList = this.disorderlyTableList.filter(item => item !== frult)
        return
      }
      if(!this.disorderlyTableList.includes(frult) && $event) {
        this.disorderlyTableList.push(frult)
        this.tableData_2.forEach((item, index) => {
          item[frult] = frult + '-' + index
        })
      }
    }
  }
}
</script>

<style scoped>
  .title {
    margin: 20px 0;
  }
  .el-row {
    margin: 10px 0;
    padding-left: 20px;
    background-color: #fff;
  }
</style>
